﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XboxUserInterface.Variables;
using XboxUserInterface.Keys;

namespace XboxUserInterface.Forms
{
    public partial class Schemes : Form
    {
        /// <summary>
        /// Creates the form
        /// </summary>
        public Schemes()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Resets the active scheme to the one which is selected in the list.
        /// </summary>
        /// <param name="sender">Schemes form sending call</param>
        /// <param name="e">Extra information for the call</param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Globals.Active = (KeyMap)listBox1.Items[listBox1.SelectedIndex];
            }
            catch
            {
                MessageBox.Show("Please select an item and try again.");
            }
        }

        /// <summary>
        /// On loading the form, updates the listbox with all schemes which have been loaded
        /// </summary>
        /// <param name="sender">Schems form sending call</param>
        /// <param name="e">Extra information for the call</param>
        private void Schemes_Load(object sender, EventArgs e)
        {
            UpdateList();
        }

        /// <summary>
        /// Updates the listbox with the current schemes loaded.
        /// </summary>
        private void UpdateList()
        {
            listBox1.Items.Clear();
            foreach (KeyMap m in Globals.Schemes)
                listBox1.Items.Add(m);

            this.Text = "Settings - Active Scheme : " + Globals.Active.ToString();
        }

        /// <summary>
        /// Opens a new blank settings window for a new scheme
        /// </summary>
        /// <param name="sender">Schems form sending call</param>
        /// <param name="e">Extra information for the call</param>
        private void button3_Click(object sender, EventArgs e)
        {
            SettingsForm f = new SettingsForm(new KeyMap(""));
            f.Show();
        }

        /// <summary>
        /// Opens a new settings window for the selected scheme
        /// </summary>
        /// <param name="sender">Schems form sending call</param>
        /// <param name="e">Extra information for the call</param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SettingsForm f = new SettingsForm(Globals.Schemes[listBox1.SelectedIndex]);
                f.Show();
            }
            catch
            {
                MessageBox.Show("Please select a list to edit");
            }
        }

        /// <summary>
        /// Removes the selected scheme from the global listing and the list
        /// </summary>
        /// <param name="sender">Schems form sending call</param>
        /// <param name="e">Extra information for the call</param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.OK == MessageBox.Show("Are you sure you want to delete this scheme?", "Delete Scheme", MessageBoxButtons.OKCancel))
                {
                    Globals.Schemes.Remove((KeyMap)listBox1.Items[listBox1.SelectedIndex]);
                    listBox1.Items.Remove(listBox1.Items[listBox1.SelectedIndex]);
                }
            }

            catch
            {
                MessageBox.Show("Please select a scheme to remove");
            }
        }

        /// <summary>
        /// Open the editor for the doubleclicked item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                SettingsForm f = new SettingsForm(Globals.Schemes[listBox1.SelectedIndex]);
                f.Show();
            }
            catch
            {
                MessageBox.Show("Please select a list to edit");
            }
        }

        private void Schemes_Enter(object sender, EventArgs e)
        {
            UpdateList();
        }

    }
}
